import sys
import os
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)

import requests
import hashlib
import urllib.parse
import time
from utils.tools import read_json

def location(AK, SK, query, region):
    base_url = "https://api.map.baidu.com/place/v2/search"
  
    # 生成SN签名
    query_dict = {
        'query': query,
        'region': region,
        'ak': AK,
        'timestamp': str(int(time.time())),
        'output': 'json'
    }
    encoded_str = urllib.parse.urlencode(query_dict)
    raw_str = f"/place/v2/search?{encoded_str}{SK}"
    sn = hashlib.md5(urllib.parse.quote_plus(raw_str).encode()).hexdigest()
  
    # 带SN的请求URL
    url = f"{base_url}?{encoded_str}&sn={sn}"
  
    response = requests.get(url)
    return response.json()

if __name__ == "__main__":
    config = read_json("path_planning/config.jsonc")
    AK = config["AK"]
    SK = config["SK"]
    query = "医院"
    region = "重庆"
    result = location(AK, SK, query, region)
    print (result)
